package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.MyPostDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 08 月 22.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.adapter
 * 工程名 welbeing_pregnant
 */
public class MeFragmentAdapter extends  RecyclerView.Adapter<MeFragmentAdapter.ViewHolder> implements View.OnClickListener  {
    private static final String TAG = "MeFragmentAdapter";
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<MyPostDateBean.DataBean> mlist = new ArrayList<>();
    private Context mcontext;
    private final String cus_logo;
    private final String cus_name;

    public MeFragmentAdapter(List<MyPostDateBean.DataBean> bean, Context context) {
        this.mcontext = context;
        SharedPreferences sp =mcontext.getSharedPreferences("second_app_internal_data", 0);
        cus_logo = sp.getString("cus_logo","");
        cus_name = sp.getString("cus_name","");
        mlist.addAll(bean);
        notifyDataSetChanged();

    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_me_recyclerview,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+cus_logo).transform(new GlideCircleTransform(mcontext)).into(holder.iv_personal_head);
       // Log.e(TAG,"onBindViewHolder===Birthday=========>"+RBConstants.SERVER_URL+"/Public/Uploads/"+cus_logo);
        holder.tv_name.setText(cus_name);
        holder.tv_date.setText(mlist.get(position).addtime);
//        String po_url = mlist.get(position).po_url;
//        if(po_url.equals("")){
//            holder.ll_user_picture.setVisibility(View.GONE);
//        }
       // String[] arr =po_url.split(",");

        holder.tv_titile.setText(mlist.get(position).po_name);
        holder.tv_content.setText(mlist.get(position).content);
       // holder.tv_classification.setText(mlist.get(position).cat_name);
        holder.tv_thumb_up.setText(mlist.get(position).count);
        holder.tv_commentaries.setText(mlist.get(position).hits_like);
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }
    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }

    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_name;
        private  TextView tv_date;
        private  TextView tv_titile;
        private  TextView tv_content;
        private  TextView tv_thumb_up;
        private  TextView tv_commentaries;
        private ImageView iv_personal_head;

        private LinearLayout ll_user_picture;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_name =  (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_name);
            tv_date =  (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_date);
            tv_titile =  (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_titile);
            tv_content =  (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_content);
            tv_thumb_up =  (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_thumb_up);
            tv_commentaries =  (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_commentaries);
            iv_personal_head =  (ImageView) itemView.findViewById(R.id.item_me_recyclerview_iv_personal_head);

        }
    }
}
